﻿// For an introduction to the Page Control template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkId=232511
(function () {
    "use strict";

    WinJS.UI.Pages.define("/pages/register/register.html", {
        // This function is called whenever a user navigates to this page. It
        // populates the page elements with the app's data.
        ready: function (element, options) {

            var b64encoded = "data:image/png;base64,";
            var username = document.getElementById("tb-username");
            var password = document.getElementById("tb-password");
            var mail = document.getElementById("tb-mail");
            var phone = document.getElementById("tb-phone");

            WinJS.Utilities.id("register").listen("click", function() {
                var persister = Persister.getPersister();
                var user = {
                    username: username.value,
                    password: password.value,
                    mail: mail.value,
                    phone: phone.vlaue,
                    image: b64encoded
                };
                persister.user.register(user);
            });


            var avatarPicker = document.getElementById("avatar");
            avatarPicker.addEventListener("click", function () {
                var openPicker = Windows.Storage.Pickers.FileOpenPicker();
                openPicker.fileTypeFilter.append(".jpg");
                openPicker.fileTypeFilter.append(".png");
                openPicker.pickSingleFileAsync().then(function (file) {
                    Windows.Storage.FileIO.readBufferAsync(file).done(function (buffer) {
                        var bytes = new Uint8Array(buffer.length);
                        var dataReader = Windows.Storage.Streams.DataReader.fromBuffer(buffer);
                        dataReader.readBytes(bytes);
                        dataReader.close();

                        b64encoded += btoa(String.fromCharCode.apply(null, bytes));
                        var img = document.createElement("img");
                        img.src = b64encoded;
                        document.getElementById("img-container").appendChild(img);
                    });
                });
            });
        },

        unload: function () {
            // TODO: Respond to navigations away from this page.
        },

        updateLayout: function (element, viewState, lastViewState) {
            /// <param name="element" domElement="true" />

            // TODO: Respond to changes in viewState.

            var displayImage = function (data) {
                var display = document.getElementById("display-iamge").winControl;
                display.viewBox(element);
            };
        }
    });
})();
